const app = getApp();
import { getShopBanner, getShopCityLocation, getShopIndex } from "../../api/shop"
import { disposeData } from "../../utils/outData";
import { programTitle } from "../../setting";
Page({
  data: {
    shopname: '',
    member_id: '',
    city_name: '',
    bannerList: [],
    page: 1,
    pageSize: 10,
    goodsList: [],
    total: 1,
    loadTip: '',
    showLoading: false,
    isFixed: false,
    showTopBtn: false,
    sort_data: 1,
    goods_name: '',
    isLoading: false
  },
  onLoad(options) {
    this.setData({
      shopname: options.shopname || disposeData().shopname,
      member_id: options.member_id || disposeData().memberId
    })
    if(options.member_id !='1644565228' || options.member_id !='1634287337'){
      my.setNavigationBar({
        title: this.data.shopname
      });
    } else {
      this.setData({shopname: programTitle})
      my.setNavigationBar({
        title: programTitle
      });
    }
    this.getLocationInfo();
    this.getBannerList();
  },
  getLocationInfo() {
    const that = this;
    getShopCityLocation(this.data.member_id).then(res => {
      my.stopPullDownRefresh();
      if (res.is_city_location == 1) {
        my.getLocation({
          type: 1,
          success(res) {
            that.setData({
              city_name: res.city.slice(0, 2)
            });
            that.getList();
          },
        });
      } else {
        that.getList();
      }
    }).catch(err => {
      my.stopPullDownRefresh();
    })
  },
  getSortList(sortType) {
    this.setData({
      sort_data: sortType
    });
    this.getList();
  },
  getList(flag) {
    this.setData({
      isLoading: true
    })
    // flag为true则加载更多
    const { member_id, city_name, sort_data, page, pageSize } = this.data;
    getShopIndex({
      member_id, city_name, goods_name: '', sort_data, page: flag ? page + 1 : 1, pageSize
    }).then(res => {
      this.setData({
        isLoading: false
      })
      let list = [];
      if (flag) {
        this.setData({
          showLoading: false
        })
        list = [...this.data.goodsList, ...res.data];
      } else {
        list = res.data;
      }
      this.setData({
        goodsList: list,
        total: res.total,
        page: res.current_page
      })
    }).catch(err => {
      this.setData({
        isLoading: false
      })
    })
  },
  onReachBottom() {
    // 页面拉到底部
    const { goodsList, total } = this.data;
    if (goodsList.length < total) {
      this.setData({
        showLoading: true
      })
      this.getList(true);
    } else if (total > 4) {
      this.setData({
        loadTip: '没有更多数据了'
      })
    }
  },
  onPageScroll(e) {
    this.setData({
      isFixed: e.scrollTop > 220,
      showTopBtn: e.scrollTop > 600
    })
  },
  onPullDownRefresh() {
    this.getLocationInfo();
    this.getBannerList();
  },
  getBannerList() {
    getShopBanner(this.data.member_id).then(res => {
      this.setData({
        bannerList: res
      })
    })
  },
  swiperClick(goods_id) {
    if (goods_id) {
      my.navigateTo({
        url: '../productDetails/productDetails?goodsId=' + goods_id+'&sale_user_id='+app.globalData.sale_user_id
      });
    }
  },
  selectCity() {
    const that = this;
    my.chooseCity({
      showLocatedCity: true,
      showHotCities: true,
      setLocatedCity: true,
      success: (res) => {
        that.setData({
          city_name: res.city,
          sort_data: 1,
          goods_name: ''
        });
        that.getList(false);
      },
    });
    // my.getLocation({
    //   type: 1,
    //   success(res) {
    //     that.setData({
    //       city_name: res.city.slice(0, 2)
    //     })
    //     my.chooseCity({
    //       showLocatedCity: true,
    //       showHotCities: true,
    //       setLocatedCity: true,
    //       success: (res) => {
    //         that.setData({
    //           city_name: res.city,
    //           sort_data: 1,
    //           goods_name: ''
    //         });
    //         that.getList(false);
    //       },
    //     });
    //   },
    // });
  },
  handleSubmit(value) {
    if (!value) {
      return;
    }
    this.setData({
      goods_name: ''
    });
    my.navigateTo({
      url: '../shop/search/search?goods_name=' + value + '&member_id=' + this.data.member_id + '&city_name=' + this.data.city_name
    })
  },
  handleInput(value) {
    this.setData({ goods_name: value });
  },
  handleClear(value) {
    this.setData({ goods_name: '' });
  },
  onShareAppMessage() {
    const { shopname, member_id } = this.data;
    return {
      title: programTitle,
      desc: '信用免押一站式租赁服务平台',
      path: 'pages/storeDetails/storeDetails?from=qrcode&shopname=' + shopname + '&member_id=' + member_id,
    };
  }
});
